<?php
session_start();
//注释：session_start() 函数必须位于 <html> 标签之前：
$source = $_GET['source'];//获取$source数据
$page = $_GET['page'];
//判断$soure数据来源
if(!$source or ($source<>'member' ) and ($source<> 'admin')){
    echo "<script>alert('页面来源错误');location.href='index.php'</script>";
    exit;
}
//判断$page是否为数字
if($page){
    if(!is_numeric($page)){
        echo "<script>alert('页面来源错误');location.href='index.php'</script>";
        exit;
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .main{
            width: 80%;
            margin:0 auto;
            text-align: center;
        }
        .main ul{
            width: 90%;
            margin: 0 auto;
            text-align: center;
        }
        h2{
            text-align: center;
            font-size: 15px;
        }
        h2 a{
            margin-right: 15px;
            color: navy;
            text-decoration: none;
        }
        h2 a:last-child{
            margin-right: 0;
        }
        h2 a:hover{
            color: crimson;
            text-decoration: none;
        }
        .current{
            color: cyan;
        }
        .formOne{
            margin-top: 40px;
            text-align: center;
        }
        .formOne input{

        }
        .red{
            color: red;
        }
    </style>
</head>
<body>
<div class="main">
    <?php

    include_once 'nav.php';
    include_once 'conn.php';

    $username = $_GET['username'];//获取管理员name
//    判断是否为管理员的name
    if($username){
//        修改管理员的name
        $sql = "select * from info where username = '".$username."'";
    }else{
//        修改登入者信息name
        $sql = "select * from info where username = '".$_SESSION['loggedUsername']."'";
    }

    $result = mysqli_query($conn, $sql);//mysql_query:查询数据库语句
    //mysqli_num_rows:返回结果集中行
    if(mysqli_num_rows($result)){
        $info = mysqli_fetch_array($result);//获取数据库的数字数据和关联数组
        $fav = explode(",", $info['fav']);//explode分割字符串变为数组
    }else{
        die("无法获取有效用户");
    }
    ?>
</div>
<form class="formOne" action="postModify.php" method="post" onsubmit="return check()">
    <table align="center" border="1" style="border-collapse: collapse" cellpadding="10" cellspacing="0">
        <tr>
            <td align="right">用户名: </td>
            <td align="left"><input name="username" readonly value=" <?php echo $info['username']; ?>"><span class="red">*</span></td>
        </tr>
        <tr>
            <td align="right">密码: </td>
            <td align="left"><input type="password"  placeholder="不修改密码请留空" name="pw" ><span class="red">*</span></td>
        </tr>
        <tr>
            <td align="right">确认密码: </td>
            <td align="left"><input type="password" name="cpm" placeholder="不修改密码请留空"><span class="red">*</span></td>
        </tr>
        <tr>
            <td align="right">性别 </td>
            <td align="left">
                <input type="radio" <?php if($info['sex']){?> checked <?php }?> name="sex" value="1" checked>男
                <input type="radio" <?php if(!$info['sex']){?> checked <?php }?> name="sex" value="0">女
            </td>
        </tr>
        <tr>
            <td align="right">邮箱: </td>
            <td align="left"><input name="email" value=" <?php echo $info['email']; ?>"></td>
        </tr>
        <tr>
            <td align="right">兴趣爱好 </td>
            <td align="left">
                <input name="fav[]" <?php if(in_array('玩游戏', $fav)){ echo "checked";}?> type="checkbox" value="玩游戏">玩游戏
                <input name="fav[]" <?php if(in_array('王者', $fav)){ echo "checked";}?> type="checkbox" value="王者">王者
                <input name="fav[]" <?php if(in_array('游戏', $fav)){ echo "checked";}?> type="checkbox" value="游戏">游戏
            </td>
        </tr>
        <tr>
            <td align="center"><input type="submit" value="提交"></td>
            <td align="left">
                <input type="reset" value="重置">
<!--                提交post的数据-->
                <input type = "hidden" name="source" value="<?php echo $source?>">
                <input type = "hidden" name="page" value="<?php echo $page?>">
            </td>
        </tr>
    </table>
</form>
<script>
    function check() {

        // let userName = document.getElementsByName('username')[0].value.trim();
        console.log(userName);
        let pw = document.getElementsByName('pw')[0].value.trim();
        let cpm = document.getElementsByName('cpm')[0].value.trim();
        let email = document.getElementsByName('email')[0].value.trim();
        // let userNameReg = /^[a-zA-Z0-9_]{3,10}$/;
        // if(!userNameReg.test(userName)){
        //     alert('用户名必须填写，且只能大小写字符和数字下划线组成，长度为3~10个字符');
        //     return false;
        // }
        if(pw.length>0) {
            let pwReg = /^[a-zA-Z0-9_*]{6,10}$/;
            if (!pwReg.test(pw)) {
                alert('密码必须填写，且只能大小写字符和数字下划线和*组成，长度为6~10个字符');
                return false;
            } else {
                if (pw != cpm) {
                    alert('密码和确认密码必须相同');
                    return false;
                }
            }
        }
        // let emailReg = /^[a-zA-Z0-9_\-]+@([a-zA-Z0-9]+\.)(com|con|arg|net)$/
        // if(email.length>0) {
        //     if (!emailReg.test(email)) {
        //         alert('邮箱格式不正确');
        //         return false;
        //     }
        // }
        let emailReg = /^[a-zA-Z0-9_\-]+@([a-zA-Z0-9]+\.)(com|cn|net|org)$/;
        if(email.length>0) {
                if (!emailReg.test(email)) {
                    alert('邮箱格式不正确');
                    return false;
                }
            }
        return true;
        // let emailReg = /^[a-zA-Z0-9_\-]+@([a-zA-Z0-9]+\.)(com|cn|net|org)$/;
        // if(email.length>0) {
        //     if (!emailReg.test(email)) {
        //         alert('邮箱格式不正确');
        //         return false;
        //     }
        // }
    }
</script>
</body>
</html>
